package com.hson.demo.base.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 请求微信平台返回结果判断
 * @author huangym3
 * @time 2015年12月9日 下午3:15:58
 */
public class WechatClientJsonUtil {

	private WechatClientJsonUtil() {}
	
	private static Logger logger = LoggerFactory.getLogger(WechatClientJsonUtil.class);
	
	/**
	 * 判断微信平台是否返回正常结果，map中没有errcode或者errcode为0时表示正常返回
	 * @param map	返回结果json数据转换成的map
	 * @return
	 */
	public static boolean isSuccess(Map<String, Object> map) {
		if (map == null || map.isEmpty()) {
			return false;
		}
		Object errcode = map.get("errcode");
		Integer code = null;
		if (errcode != null) {
			code = (Integer) errcode;
		}
		return (code == null || code == 0);
	}
	
	/**
	 * Linux用命令调用其他程序
	 * @param commandStr
	 * @return
	 */
	public static String exec(String commandStr) {
		logger.info(commandStr);
		Process process;
		StringBuilder sb = new StringBuilder();
		InputStreamReader reader = null;
		InputStreamReader readErr = null;
		LineNumberReader line = null;
		LineNumberReader lineErr = null;
		try {
			process = Runtime.getRuntime().exec(commandStr);
			process.waitFor();
			reader = new InputStreamReader(process.getInputStream());
			readErr = new InputStreamReader(process.getErrorStream());
			line = new LineNumberReader(reader);
			lineErr = new LineNumberReader(readErr);
			logger.error("start reader exec error output......");
			String str = "";
			while ((str = lineErr.readLine()) != null) {
				sb.append(str).append("\n");
			}
			logger.error("+++++++++++++++++++++++++++++++++++++++\n" + sb.toString());
			sb = new StringBuilder();
			logger.info("start reader exec output......");
			while ((str = line.readLine()) != null) {
				sb.append(str).append("\n");
			}
			logger.info("+++++++++++++++++++++++++++++++++++++++\n" + sb.toString());
			return sb.toString();
		} catch (IOException e) {
			logger.info("+++++++++++++++--------------------------++++++++++++++++++++++++" + e.getMessage());
		} catch (InterruptedException e) {
			logger.info("+++++++++++++++--------------------------++++++++++++++++++++++++" + e.getMessage());
		} finally {
			if (lineErr != null) {
				try {
					lineErr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (readErr != null) {
				try {
					readErr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (line != null) {
				try {
					line.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	/**
	 * Linux用命令调用其他程序，输出信息
	 * @param commandStr
	 * @return
	 */
	public static String execOutput(String commandStr) {
		logger.info(commandStr);
		Process process;
		StringBuilder sb = new StringBuilder();
		InputStreamReader reader = null;
		LineNumberReader line = null;
		try {
			process = Runtime.getRuntime().exec(commandStr);
			process.waitFor();
			reader = new InputStreamReader(process.getInputStream());
			line = new LineNumberReader(reader);
			logger.info("start reader exec output......");
			String str = "";
			while ((str = line.readLine()) != null) {
				sb.append(str).append("\n");
			}
			logger.info("+++++++++++++++++++++++++++++++++++++++\n" + sb.toString());
			return sb.toString();
		} catch (IOException e) {
			logger.info("+++++++++++++++--------------------------++++++++++++++++++++++++" + e.getMessage());
		} catch (InterruptedException e) {
			logger.info("+++++++++++++++--------------------------++++++++++++++++++++++++" + e.getMessage());
		} finally {
			if (line != null) {
				try {
					line.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	/**
	 * Linux用命令调用其他程序，输出error信息
	 * @param commandStr
	 * @return
	 */
	public static String execOutputError(String commandStr) {
		logger.info(commandStr);
		Process process;
		StringBuilder sb = new StringBuilder();
		InputStreamReader readErr = null;
		LineNumberReader lineErr = null;
		try {
			process = Runtime.getRuntime().exec(commandStr);
			process.waitFor();
			readErr = new InputStreamReader(process.getErrorStream());
			lineErr = new LineNumberReader(readErr);
			logger.error("start reader exec error output......");
			String str = "";
			while ((str = lineErr.readLine()) != null) {
				sb.append(str).append("\n");
			}
			logger.info("+++++++++++++++++++++++++++++++++++++++\n" + sb.toString());
			return sb.toString();
		} catch (IOException e) {
			logger.info("+++++++++++++++--------------------------++++++++++++++++++++++++" + e.getMessage());
		} catch (InterruptedException e) {
			logger.info("+++++++++++++++--------------------------++++++++++++++++++++++++" + e.getMessage());
		} finally {
			if (lineErr != null) {
				try {
					lineErr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (readErr != null) {
				try {
					readErr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return "";
	}
}
